//
//  ViewController.m
//  手机通讯录
//
//  Created by Roach on 16/6/17.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, SecondViewControllerDelegate>

@property (nonatomic, retain) NSDictionary *dictionary;
@property (nonatomic, retain) NSArray *kArray;

@property (nonatomic, retain) NSString *strName;

@end

@implementation ViewController

- (void)dealloc {
    [_strName release];
    [_cell release];
    [_dictionary release];
    [_kArray release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self config];
    [self data];
    [self creatTableView];
}

- (void)config {
    
    self.dictionary = [[NSDictionary alloc]init];
    self.kArray = [[NSArray alloc]init];
    self.cell = [[UITableViewCell alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"联系人";
}

// 数据处理
- (void)data {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Content" ofType:@"plist"];
    
    self.dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    self.kArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    
}

- (void)creatTableView {
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.rowHeight =80;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    

    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *key = [self.kArray objectAtIndex:section];
    
    NSArray *array = [self.dictionary objectForKey:key];
    
    return array.count;

    
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.dictionary objectForKey:[self.kArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    _cell.textLabel.text = [dic objectForKey:@"name"];
    _cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"header"]];
    _cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    
    return _cell;
    
}

// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dictionary.count;
    
}

// 分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *headerString = [self.kArray objectAtIndex:section];
    return headerString;
    
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.kArray;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    
    NSLog(@"%@", self.cell.textLabel.text);
    
    NSArray *array = [self.dictionary objectForKey:[self.kArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    self.strName = [dic objectForKey:@"name"];
//    self.strName = @"hah ";
    
    
    NSDictionary *dic1 = [array objectAtIndex:indexPath.row];
    NSString *strImage = [dic1 objectForKey:@"header"];
    
    NSDictionary *dic2 = [array objectAtIndex:indexPath.row];
    NSString *strNumber = [dic2 objectForKey:@"number"];
    
    NSDictionary *dic3 = [array objectAtIndex:indexPath.row];
    NSString *strQq = [dic3 objectForKey:@"QQ"];
    
    secondVC.nametextField = _strName;
    secondVC.numbertextField = strNumber;
    secondVC.QQtextField = strQq;
    secondVC.headerImage = strImage;

    NSLog(@"%@", secondVC.nametextField);
    
//    secondVC.nametextField = _cell.textLabel.text;
    
    secondVC.delegate = self;
    
    [self.navigationController pushViewController:secondVC animated:YES];
    
}

// 协议方法
- (void)sendText:(NSString *)text {
    
     self.cell.textLabel.text = text;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
